/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.fty.web;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.XMLWorkerHelper;
import com.jeesite.common.entity.DataScope;
import com.jeesite.common.mybatis.mapper.query.QueryType;

import com.jeesite.common.utils.IdcardUtils;
import com.jeesite.modules.*;
import com.jeesite.modules.sys.entity.UserDataScope;
import com.jeesite.modules.sys.utils.*;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.fty.entity.FtySelContract;
import com.jeesite.modules.fty.service.FtySelContractService;

import java.beans.IntrospectionException;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.nio.charset.Charset;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;

/**
 * 合同表Controller
 * @author chensk
 * @version 2018-04-17
 */
@Controller
@RequestMapping(value = "${adminPath}/fty/ftySelContract")
public class FtySelContractController extends BaseController {

	@Autowired
	private FtySelContractService ftySelContractService;

	/**
	 * 获取数据
	 */
	@ModelAttribute
	public FtySelContract get(String contractCode, boolean isNewRecord) {

		FtySelContract contract = ftySelContractService.get(contractCode, isNewRecord);
		return contract;

	}

	/**
	 * 查询列表
	 */
	@RequiresPermissions("fty:ftySelContract:view")
	@RequestMapping(value = {"list", ""})
	public String list(FtySelContract ftySelContract, Model model,HttpServletRequest request) {

		model.addAttribute("ftySelContract", ftySelContract);
		if(HttpRequestDeviceUtils.isMobileDevice(request)){
			request.setAttribute("mobile",1);

		}else{
			request.setAttribute("mobile",0);
		}
		return "modules/fty/ftySelContractList";
	}

	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("fty:ftySelContract:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<FtySelContract> listData(Date startDate,Date endDate,FtySelContract ftySelContract, HttpServletRequest request, HttpServletResponse response) {



		ftySelContract.setCreateDate_lte(endDate);
		ftySelContract.setCreateDate_gte(startDate);
		ftySelContractService.addDataScopeFilter(ftySelContract,null);
		if( UserUtils.getUser().getUserCode().equals("admin")){
			ftySelContract.getSqlMap().getWhere().and("c_number", QueryType.IS_NOT_NULL, null);
		}else{


		}


		Page<FtySelContract> page = ftySelContractService.findPage(new Page<FtySelContract>(request, response), ftySelContract);
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("fty:ftySelContract:view")
	@RequestMapping(value = "form")
	public String form(FtySelContract ftySelContract, Model model) {


		model.addAttribute("ftySelContract", ftySelContract);
		return "modules/fty/ftySelContractForm";
	}

	/**
	 * 保存合同表
	 */
	@RequiresPermissions("fty:ftySelContract:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated FtySelContract ftySelContract,String[] otherEngagements,String [] jfYd) {


		if(!IdcardUtils.validateCard(ftySelContract.getBIdcard())){
			return renderResult(Global.FALSE, "乙方身份证号码错误");
		}

		if(StringUtils.isNotBlank(ftySelContract.getId())){
			if(!ftySelContract.getCreateBy().equals(EmpUtils.getEmployee().getEmpCode()))
				return renderResult(Global.FALSE, "您不是合同原件创建人,无法保存合同");
		}

		ftySelContract.setCName(EmpUtils.getCompany().getFullName());
		ftySelContract.setCLegal(EmpUtils.getCompany().getRepresentive());
		ftySelContract.setCAddress(EmpUtils.getCompany().getOfficeAddress());
		ftySelContract.setCPostcode(EmpUtils.getCompany().getZipcode());
		ftySelContract.setCBusiCode(EmpUtils.getCompany().getSnCode());
		ftySelContract.setCPhone(EmpUtils.getCompany().getHeadTel());
		ftySelContract.setCRecordNumber(EmpUtils.getCompany().getRecordNum());
		ftySelContract.setCPhone(EmpUtils.getCompany().getHeadTel());
		ftySelContract.setCPName(EmpUtils.getEmployee().getEmpName());
		ftySelContract.setCPPhone(EmpUtils.getEmployee().getTel());
		ftySelContract.setCPIdCard(EmpUtils.getEmployee().getCardNo());
		ftySelContract.setCTsPhone(EmpUtils.getCompany().getHeadTel());
		ftySelContract.setCBroCer(EmpUtils.getEmployee().getCertificate());


		if(StringUtils.isBlank(ftySelContract.getHMoneyFive()))
			ftySelContract.setHMoneyFive("0");
		if(StringUtils.isBlank(ftySelContract.getHMoneyFour()))
			ftySelContract.setHMoneyFour("0");
		if(StringUtils.isBlank(ftySelContract.getHMoneyFirst()))
			ftySelContract.setHMoneyFirst("0");

		if(StringUtils.isBlank(ftySelContract.getHMoneySecond()))
			ftySelContract.setHMoneySecond("0");
		if(StringUtils.isBlank(ftySelContract.getHMoneyThird()))
			ftySelContract.setHMoneyThird("0");


		double moneyall = Double.valueOf(ftySelContract.getHMoneyAll());
		double hmoneyfirst = Double.valueOf(ftySelContract.getHMoneyFirst());//定金
		double hMoneySecond= Double.valueOf(ftySelContract.getHMoneySecond());//首付
		double hMoneyThird = Double.valueOf(ftySelContract.getHMoneyThird()); //第二次
		double hMoneyFour = Double.valueOf(ftySelContract.getHMoneyFour());//余款
		double hMoneyFive = Double.valueOf(ftySelContract.getHMoneyFive());//按揭余款

		if(moneyall!=hmoneyfirst+hMoneyThird+hMoneySecond+hMoneyFour+hMoneyFive){
			return renderResult(Global.FALSE, "总价为"+moneyall+",而所有金额相加只有"+(hmoneyfirst+hMoneyThird+hMoneySecond+hMoneyFour+hMoneyFive));
		}




		if(StringUtils.isNotBlank(ftySelContract.getCNumber())){

			return renderResult(Global.FALSE, "已经生成序列号 无法修改");
		}


		ftySelContractService.save(ftySelContract);
		return renderResult(Global.TRUE, "保存合同表成功！");
	}

	/**
	 * 删除合同表
	 */
	@RequiresPermissions("fty:ftySelContract:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(FtySelContract ftySelContract) {
		if(StringUtils.isNotBlank(ftySelContract.getCNumber())){
			return renderResult(Global.FALSE, "有序列号的合同不能删除,请联系总部！");
		}
		ftySelContractService.delete(ftySelContract);
		return renderResult(Global.TRUE, "删除合同表成功！");
	}


	@RequestMapping(value = {"generalNumber"})
	@ResponseBody
	public String generalNumber(FtySelContract ftySelContract, Model model) {

	//	#h_scdate#

		SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日");
		ftySelContract.setHScdate(format.format(System.currentTimeMillis()));

		if(!ftySelContract.getCreateBy().equals(EmpUtils.getEmployee().getEmpCode()))
			return renderResult(Global.FALSE, "您不是合同原件创建人,无法生成合同");
		if(StringUtils.isNotBlank(ftySelContract.getCNumber()))
			return renderResult(Global.FALSE, "有序列号的合同已经生成,无法改变");

		ftySelContractService.save(ftySelContract);
		ftySelContractService.generalUUid(ftySelContract);

		return renderResult(Global.TRUE, "生成合同编号成功！");
	}
	@RequestMapping("/mobile")
	public String mobile(FtySelContract ftySelContract, HttpServletRequest request,
								 HttpServletResponse response, HttpSession session)
			throws DocumentException, IOException, ParseException, IllegalAccessException, IntrospectionException, InvocationTargetException {

		Map<String,Object> map = BeanMapUtil.bean2Map(ftySelContract);



		response.setContentType("application/pdf");
		response.setHeader("Expires", "0");// 不缓存
		response.setHeader("Cache-Control",
				"must-revalidate, post-check=0, pre-check=0");
		response.setHeader("Pragma", "public");

		// 如果不想立即下载，屏蔽下面一行，就是浏览器中浏览了
		// response.setHeader("Content-disposition",
		// "attachment; filename = contract.pdf");
		String contract = Global.getConfig("sys.contract.content", "10");

		//要替换的字符名称
		String lender = "";
		try {
			Document document = new Document();
			ServletOutputStream out = response.getOutputStream();
			PdfWriter writer = PdfWriter.getInstance(document, out);// 输出到浏览器
			String  c = ftySelContract.getCName()+ftySelContract.getCNumber();

			if(StringUtils.isBlank(ftySelContract.getCNumber())){

				c = "预览模式,打印无效" ;

			}

			writer.setPageEvent(new Watermark(c));//水印内容 添加事件 在事件中添加水印
			// 若果是要输出到文件，取消下面的注释，屏蔽response的设置
			// PdfWriter writer = PdfWriter.getInstance(document,new
			// FileOutputStream("protocol.pdf"));
			document.open();

			String importantClause = ftySelContract.getImportantClause();

			contract = ReplaceUtils.replaceHd(contract,ftySelContract);

			String contract_spacial = ReplaceUtils.getDicts("contract_special",importantClause);
			String contract_add = ReplaceUtils.getDicts("contract_add",ftySelContract.getContractAdd());
			String contract_fy_others = ReplaceUtils.getDicts("contract_fy_others",ftySelContract.getFyOthers());
			DecimalFormat df   = new DecimalFormat("######0.00");
			Double b = 0.00D;

			Double a = 0.00D;


			if(isNumeric(ftySelContract.getBManageFee())){
				b = Double.parseDouble(ftySelContract.getBManageFee());
				contract  = contract.replaceAll("\\#b_manage_fee\\#", df.format(b)+"");
			}else{
				contract  = contract.replaceAll("\\#b_manage_fee\\#",ftySelContract.getBManageFee());
			}
			if(isNumeric(ftySelContract.getAManageFee())){
				a = Double.parseDouble(ftySelContract.getAManageFee());
				contract  = contract.replaceAll("\\#a_manage_fee\\#", df.format(a)+"");
			}else{
				contract  = contract.replaceAll("\\#a_manage_fee\\#", ftySelContract.getAManageFee());
			}
			if(isNumeric(ftySelContract.getAManageFee())&&isNumeric(ftySelContract.getBManageFee())){
				contract = contract.replaceAll("\\#manage_fee_all\\#", df.format(Double.valueOf(map.get("a_manage_fee")+"")+Double.valueOf(map.get("b_manage_fee")+"")));
			}else{
				contract  = contract.replaceAll("\\#manage_fee_all\\#", "按买卖双方实付总额");
			}

			contract  = contract.replaceAll("\\#contract_add\\#", ReplaceUtils.replacewithSpan(contract_add));
			contract  = contract.replaceAll("\\#fy_others\\#", ReplaceUtils.replacewithSpan(contract_fy_others));
			contract  = contract.replaceAll("\\#special_import\\#", ReplaceUtils.replacewithSpan(contract_spacial));
			//文字水印
			//添加水印和页码
			contract  = contract.replaceAll("\\#important_clause\\#", ReplaceUtils.replace(ftySelContract.getJfYd()));
			contract  = contract.replaceAll("\\#jf_yd\\#", ReplaceUtils.replace(ftySelContract.getJfYd()));
			contract  = contract.replaceAll("\\#other_engagements\\#", ReplaceUtils.replace(ftySelContract.getOtherEngagements()));
			contract  = contract.replaceAll("\\#h_others_status\\#", StringUtils.isBlank(ftySelContract.getHOthersStatus())?"":ftySelContract.getHOthersStatus().replaceAll(",",""));
			contract  = contract.replaceAll("\\#jf_others\\#", ReplaceUtils.replace(ftySelContract.getJfOthers()==null?"/":ftySelContract.getJfOthers()));
			contract  = contract.replaceAll("\\#apn\\#", StringUtils.isBlank(ftySelContract.getAPName())?"/":ftySelContract.getAPName());
			contract  = contract.replaceAll("\\#bpn\\#",StringUtils.isBlank(ftySelContract.getBPName())?"/":ftySelContract.getBPName());
			contract  = contract.replaceAll("\\#bpc\\#", StringUtils.isBlank(ftySelContract.getBPContry())?"/":ftySelContract.getBPContry());
			contract  = contract.replaceAll("\\#apc\\#", StringUtils.isBlank(ftySelContract.getAPContry())?"/":ftySelContract.getAPContry());
			contract  = contract.replaceAll("\\#bpct\\#", StringUtils.isBlank(ftySelContract.getBPType())?"/":ftySelContract.getBPType());
			contract  = contract.replaceAll("\\#apct\\#", StringUtils.isBlank(ftySelContract.getAPCardType())?"/":ftySelContract.getAPCardType());
			contract  = contract.replaceAll("\\#bpi\\#", StringUtils.isBlank(ftySelContract.getBPIdcard())?"/":ftySelContract.getBPIdcard());
			contract  = contract.replaceAll("\\#api\\#", StringUtils.isBlank(ftySelContract.getAPIdcard())?"/":ftySelContract.getAPIdcard());

			if(StringUtils.isBlank(ftySelContract.getHOthers())){
				ftySelContract.setHOthers("/");
			}
			//			DictUtils.getDictLabel();
			//String[] values = importantClause.split(",");
			//contract = contract.replaceAll("\\#special_import\\#",);
			String string="hello ";
			string=string.replace(" ","\u00a0");
			InputStream cssStream = request.getServletContext().getResourceAsStream("/static/fang/style/word.css");
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");


			for (Map.Entry<String, Object> entry : map.entrySet()) {
				String key = entry.getKey().toLowerCase();
				Object value = entry.getValue();
				if(entry.getValue() instanceof Date){
					value = formatter.format(value);
				}

				contract  = contract.replaceAll("\\#"+key+"\\#", StringUtils.isBlank(value+"")?"<span style=\"color:white\">00</span>/":String.valueOf(value));


			}


			if( (map.get("jf_date") instanceof Date)&&map.get("jf_date") !=null) {
				contract = contract.replaceAll("\\#jf_date\\#", formatter.format(map.get("jf_date")));
			}
			for (Map.Entry<String, Object> entry : map.entrySet()) {
				String key = entry.getKey().toLowerCase();
				Object value = entry.getValue();
				contract  = contract.replaceAll("\\#"+key+"\\#", StringUtils.isBlank(value+"")?"/":String.valueOf(value));
			}
			contract = contract.replaceAll("\\#a_yq_oneday_money\\#", ftySelContract.getAYqOneydayMoney());

			contract = contract.replaceAll("\\#c_name_full\\#", ftySelContract.getCName());
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
			contract = contract.replaceAll("\\#c_date\\#",format.format(ftySelContract.getCreateDate()) );
			contract = contract.replaceAll("\\#b_p_poscode\\#",ftySelContract.getBPPostcode());
			contract = contract.replaceAll("\\#b_p_card_type\\#",ftySelContract.getBPType());
			contract  = contract.replaceAll("\\#h_money_all_cn\\#", RmbUtil.number2CNMontrayUnit(new BigDecimal(map.get("h_money_all")+"")));



			ByteArrayInputStream bin = new ByteArrayInputStream(contract.getBytes());
			XMLWorkerHelper.getInstance().parseXHtml(writer,document, bin,  cssStream, new ChinaFontProvide());
			document.close();
			out.close();


		} catch (DocumentException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static boolean isNumeric(String str)
	{
		for (int i = 0; i < str.length(); i++)
		{
		System.out.println(str.charAt(i));
		if (!Character.isDigit(str.charAt(i)))
	{
			return false;
			}
		}
		return true;
	}


	@RequestMapping("/sellercontract")
		public String sellercontract(FtySelContract ftySelContract, HttpServletRequest request,
								   HttpServletResponse response, HttpSession session)
				throws DocumentException, IOException, ParseException, IllegalAccessException, IntrospectionException, InvocationTargetException {
		if(HttpRequestDeviceUtils.isMobileDevice(request)){
			request.setAttribute("url","/a/fty/ftySelContract/mobile?contractCode="+ftySelContract.getContractCode());
			return "modules/fty/ftySelContractMobile";
		}

			Map<String,Object> map = BeanMapUtil.bean2Map(ftySelContract);



			response.setContentType("application/pdf");
			response.setHeader("Expires", "0");// 不缓存
			response.setHeader("Cache-Control",
					"must-revalidate, post-check=0, pre-check=0");
			response.setHeader("Pragma", "public");

			// 如果不想立即下载，屏蔽下面一行，就是浏览器中浏览了
			// response.setHeader("Content-disposition",
			// "attachment; filename = contract.pdf");
			String contract = Global.getConfig("sys.contract.content", "10");

			//要替换的字符名称
			String lender = "";
			try {
				Document document = new Document();
				ServletOutputStream out = response.getOutputStream();
				PdfWriter writer = PdfWriter.getInstance(document, out);// 输出到浏览器
				String  c = ftySelContract.getCName()+ftySelContract.getCNumber();

				if(StringUtils.isBlank(ftySelContract.getCNumber())){

					c = "预览模式,打印无效" ;

				}

				writer.setPageEvent(new Watermark(c));//水印内容 添加事件 在事件中添加水印
				// 若果是要输出到文件，取消下面的注释，屏蔽response的设置
				// PdfWriter writer = PdfWriter.getInstance(document,new
				// FileOutputStream("protocol.pdf"));
				document.open();

				String importantClause = ftySelContract.getImportantClause();

				contract = ReplaceUtils.replaceHd(contract,ftySelContract);

				String contract_spacial = ReplaceUtils.getDicts("contract_special",importantClause);
				String contract_add = ReplaceUtils.getDicts("contract_add",ftySelContract.getContractAdd());
				String contract_fy_others = ReplaceUtils.getDicts("contract_fy_others",ftySelContract.getFyOthers());
				DecimalFormat df   = new DecimalFormat("######0.00");
				Double b = 0.00D;

				Double a = 0.00D;


				if(isNumeric(ftySelContract.getBManageFee())){
					b = Double.parseDouble(ftySelContract.getBManageFee());
					contract  = contract.replaceAll("\\#b_manage_fee\\#", df.format(b)+"");
				}else{
					contract  = contract.replaceAll("\\#b_manage_fee\\#",ftySelContract.getBManageFee());
				}
				if(isNumeric(ftySelContract.getAManageFee())){
					a = Double.parseDouble(ftySelContract.getAManageFee());
					contract  = contract.replaceAll("\\#a_manage_fee\\#", df.format(a)+"");
				}else{
					contract  = contract.replaceAll("\\#a_manage_fee\\#", ftySelContract.getAManageFee());
				}
				if(isNumeric(ftySelContract.getAManageFee())&&isNumeric(ftySelContract.getBManageFee())){
					contract = contract.replaceAll("\\#manage_fee_all\\#", df.format(Double.valueOf(map.get("a_manage_fee")+"")+Double.valueOf(map.get("b_manage_fee")+"")));
				}else{
					contract  = contract.replaceAll("\\#manage_fee_all\\#", "按买卖双方实付总额");
				}

				contract  = contract.replaceAll("\\#contract_add\\#", ReplaceUtils.replacewithSpan(contract_add));
				contract  = contract.replaceAll("\\#fy_others\\#", ReplaceUtils.replacewithSpan(contract_fy_others));
				contract  = contract.replaceAll("\\#special_import\\#", ReplaceUtils.replacewithSpan(contract_spacial));
				//文字水印
				//添加水印和页码
				contract  = contract.replaceAll("\\#important_clause\\#", ReplaceUtils.replace(ftySelContract.getJfYd()));
				contract  = contract.replaceAll("\\#jf_yd\\#", ReplaceUtils.replace(ftySelContract.getJfYd()));
				contract  = contract.replaceAll("\\#other_engagements\\#", ReplaceUtils.replace(ftySelContract.getOtherEngagements()));
				contract  = contract.replaceAll("\\#h_others_status\\#", StringUtils.isBlank(ftySelContract.getHOthersStatus())?"":ftySelContract.getHOthersStatus().replaceAll(",",""));
				contract  = contract.replaceAll("\\#jf_others\\#", ReplaceUtils.replace(ftySelContract.getJfOthers()==null?"/":ftySelContract.getJfOthers()));
				contract  = contract.replaceAll("\\#apn\\#", StringUtils.isBlank(ftySelContract.getAPName())?"/":ftySelContract.getAPName());
				contract  = contract.replaceAll("\\#bpn\\#",StringUtils.isBlank(ftySelContract.getBPName())?"/":ftySelContract.getBPName());
				contract  = contract.replaceAll("\\#bpc\\#", StringUtils.isBlank(ftySelContract.getBPContry())?"/":ftySelContract.getBPContry());
				contract  = contract.replaceAll("\\#apc\\#", StringUtils.isBlank(ftySelContract.getAPContry())?"/":ftySelContract.getAPContry());
				contract  = contract.replaceAll("\\#bpct\\#", StringUtils.isBlank(ftySelContract.getBPType())?"/":ftySelContract.getBPType());
				contract  = contract.replaceAll("\\#apct\\#", StringUtils.isBlank(ftySelContract.getAPCardType())?"/":ftySelContract.getAPCardType());
				contract  = contract.replaceAll("\\#bpi\\#", StringUtils.isBlank(ftySelContract.getBPIdcard())?"/":ftySelContract.getBPIdcard());
				contract  = contract.replaceAll("\\#api\\#", StringUtils.isBlank(ftySelContract.getAPIdcard())?"/":ftySelContract.getAPIdcard());

				if(StringUtils.isBlank(ftySelContract.getHOthers())){
					ftySelContract.setHOthers("/");
				}
	//			DictUtils.getDictLabel();
				//String[] values = importantClause.split(",");
				//contract = contract.replaceAll("\\#special_import\\#",);
				String string="hello ";
				string=string.replace(" ","\u00a0");
				InputStream cssStream = request.getServletContext().getResourceAsStream("/static/fang/style/word.css");
				SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");


					for (Map.Entry<String, Object> entry : map.entrySet()) {
						String key = entry.getKey().toLowerCase();
						Object value = entry.getValue();
						if(entry.getValue() instanceof Date){
							 value = formatter.format(value);
						}

						contract  = contract.replaceAll("\\#"+key+"\\#", StringUtils.isBlank(value+"")?"<span style=\"color:white\">00</span>/":String.valueOf(value));


					}


				if( (map.get("jf_date") instanceof Date)&&map.get("jf_date") !=null) {
					contract = contract.replaceAll("\\#jf_date\\#", formatter.format(map.get("jf_date")));
				}
				for (Map.Entry<String, Object> entry : map.entrySet()) {
					String key = entry.getKey().toLowerCase();
					Object value = entry.getValue();
					contract  = contract.replaceAll("\\#"+key+"\\#", StringUtils.isBlank(value+"")?"/":String.valueOf(value));
				}
				contract = contract.replaceAll("\\#a_yq_oneday_money\\#", ftySelContract.getAYqOneydayMoney());

				contract = contract.replaceAll("\\#c_name_full\\#", ftySelContract.getCName());
				SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
				contract = contract.replaceAll("\\#c_date\\#",format.format(ftySelContract.getCreateDate()) );
				contract = contract.replaceAll("\\#b_p_poscode\\#",ftySelContract.getBPPostcode());
				contract = contract.replaceAll("\\#b_p_card_type\\#",ftySelContract.getBPType());
				contract  = contract.replaceAll("\\#h_money_all_cn\\#", RmbUtil.number2CNMontrayUnit(new BigDecimal(map.get("h_money_all")+"")));



				ByteArrayInputStream bin = new ByteArrayInputStream(contract.getBytes());
				XMLWorkerHelper.getInstance().parseXHtml(writer,document, bin,  cssStream, new ChinaFontProvide());
				document.close();
				out.close();

			} catch (Exception e) {
				logger.error("error",e.toString());
				e.printStackTrace();
			}
		return null;
		}


}